package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.IUserRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.swagger2.Person;
import cn.wolfcode.wolf2w.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("users")
//@Api：用在类上，说明该类的作用
@Api(value = "用户资源", description = "用户资源控制器")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserRedisService userRedisService;

    @Autowired
    private IStrategyStatisVORedisService statisVORedisService;

    //测试
    @GetMapping("/get")
    public Object get(String id) {
        return userInfoService.get(id);
    }

    //判断手机号码是否存在
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    //注册,发送短信验证码
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }

    //@ApiOperation：用在方法上，说明方法的作用
    @ApiOperation(value = "注册功能", notes = "其实就是新增用户")
    //@ApiImplicitParams：用在方法上包含一组参数说明
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机", name = "phone", dataType = "String", required = true),
            @ApiImplicitParam(value = "昵称", name = "nickname", dataType = "String", required = true),
            @ApiImplicitParam(value = "密码", name = "password", dataType = "String", required = true),
            @ApiImplicitParam(value = "确认密码", name = "rpassword", dataType = "String", required = true),
            @ApiImplicitParam(value = "验证", name = "verifyCode", dataType = "String", required = true)
    })
    //用户注册
    @PostMapping("/regist")
    public Object regist(String phone, String nickname,
                         String password, String rpassword, String verifyCode) {
        userInfoService.verifyCode(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }


    //登录
    @PostMapping("/login")
    public Object login(String username, String password) {
        //验证用户账号密码
        UserInfo user = userInfoService.login(username, password);
        //设置token令牌
        String token = userRedisService.setToken(user);
        //前端定义map类型接收数据
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", user);
        return JsonResult.success(map);
    }

    //第二次访问
    //表示该方法需要登录验证才可以访问
    @RequireLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfo userInfo = userRedisService.getUserByToken(token);
        return JsonResult.success(userInfo);
    }


    //查询用户收藏的id集合,数据做显示收藏颜色用
    @GetMapping("/strategies/favor")
    public Object favor(String sid, String userId) {
        List<String> sidList = statisVORedisService.getUserFavorList(sid, userId);
        return JsonResult.success(sidList);
    }


    //测试swagger
    @ApiOperation(value = "swagger测试", notes = "swagger测试")
    @GetMapping("/swagger/test")
    public Object person(Person person) {
        return JsonResult.success();
    }

}

